package com.zfei.gfb.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.zfei.gfb.bean.DataMap;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbGoodsMapper;
import com.zfei.gfb.dao.GfbOrderMapper;
import com.zfei.gfb.dao.GfbVillagerMapper;
import com.zfei.gfb.model.GfbGoods;
import com.zfei.gfb.model.GfbOrder;
import com.zfei.gfb.model.GfbVillager;
import com.zfei.gfb.query.CommonIdQuery;
import com.zfei.gfb.query.GfbOrderQuery;
import com.zfei.gfb.request.GfbOrderRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Gsons;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.vo.GfbJobWantedVo;
import com.zfei.gfb.vo.GfbOrderVo;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 * @version 1.0
 */
@RestController
@RequestMapping("/h5/order")
public class H5OrderController {

    @Resource
    private GfbOrderMapper gfbOrderMapper;

    @Resource
    private GfbGoodsMapper gfbGoodsMapper;

    @Resource
    private GfbVillagerMapper gfbVillagerMapper;


    @LogResult
    @HttpJson("/list")
    public Object list(@RequestBody JsonObject req) {
        GfbOrderQuery query = Gsons.fromJson(req, GfbOrderQuery.class);
        query.build(Now::millis);

        DataMap<String, Object> result = DataMap.build();

        int total = gfbOrderMapper.total(query);
        result.add("total", total);
        if (total == 0) {
            return result.add("orders", Lists.newArrayList());
        }
        List<GfbOrder> list = gfbOrderMapper.list(query);
        List<GfbOrderVo> gfbOrderVos = Beans.copys(list, GfbOrderVo.class);
        result.add("orders", gfbOrderVos);
        return result;

    }

    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = H5OrderVo.class),
    })
    @HttpJson("/detail")
    public Object detail(@RequestBody CommonIdQuery query) {

        Integer id = query.getId();
        if (id == null) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }

        GfbOrder gfbOrder = gfbOrderMapper.selectByPrimaryKey(id);
        if (gfbOrder == null) {
            throw new StatusException(Status.NOT_EXIST);
        }

        GfbGoods goods = gfbGoodsMapper.selectByPrimaryKey(gfbOrder.getGoodsId());
        if (goods == null) {
            throw new StatusException(Status.NOT_EXIST);
        }
        H5OrderVo orderVo = new H5OrderVo();
        orderVo.setOrder(gfbOrder);
        orderVo.setGoods(goods);

        GfbVillager goodsOwner = gfbVillagerMapper.selectByPrimaryKey(goods.getUserId());
        orderVo.setGoodsOwner(goodsOwner);

        return orderVo;
    }

    @LogResult
    @HttpJson("/add")
    public Object add(@RequestBody JsonObject req) {

        GfbOrderRequest request = Gsons.fromJson(req, GfbOrderRequest.class);
        request.checkInsertNull();
        GfbOrder record = Beans.copy(request, GfbOrder.class);
        record.setCreateTime(Now.millis());
        record.setUpdateTime(Now.millis());
        record.setCntime(DateUtil.now());

        gfbOrderMapper.insertSelective(record);
        return null;
    }


    @Data
    public static class H5OrderVo {

        @ApiModelProperty(value = "订单信息")
        private GfbOrder order;
        @ApiModelProperty(value = "订单关联的商品信息")
        private GfbGoods goods;

        @ApiModelProperty(value = "物主村民信息")
        private GfbVillager goodsOwner;

    }
}
